
package com.rt.schedulenew.utils.coder;

import java.io.UnsupportedEncodingException;

public class Hex {
    public static String DEFAULT_CHARSET_NAME = "UTF-8";
    private static char[] DIGITS_LOWER;
    private static char[] DIGITS_UPPER;
    private static String charsetName;

    public static byte[] decodeHex(char[] data) throws Exception {
        int len = data.length;
        if ((len & 0x1) != 0x0) {
            throw new Exception("Odd number of characters.");
        }
        byte[] out = new byte[len >> 1];
        int f;
        for (int i = 0, j = 0; j < len; ++j, f |= toDigit(data[j], j), ++j, out[i] = (byte) (f & 0xFF), ++i) {
            f = toDigit(data[j], j) << 4;
        }
        return out;
    }

    public static char[] encodeHex(byte[] data) {
        return encodeHex(data, true);
    }

    public static char[] encodeHex(byte[] data, boolean toLowerCase) {
        return encodeHex(data, toLowerCase ? Hex.DIGITS_LOWER : Hex.DIGITS_UPPER);
    }

    protected static char[] encodeHex(byte[] data, char[] toDigits) {
        int l = data.length;
        char[] out = new char[l << 1];
        int i = 0;
        int j = 0;
        while (i < l) {
            out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
            out[j++] = toDigits[0xF & data[i]];
            ++i;
        }
        return out;
    }

    public static String encodeHexString(byte[] data) {
        return new String(encodeHex(data));
    }

    protected static int toDigit(char ch, int index) throws Exception {
        int digit = Character.digit(ch, 16);
        if (digit == -1) {
            throw new Exception("Illegal hexadecimal charcter " + ch + " at index " + index);
        }
        return digit;
    }

    public Hex() {
    }

    public Hex(String csName) {
        Hex.charsetName = csName;
    }

    public byte[] decode(byte[] array) throws Exception {
        try {
            return decodeHex(new String(array, this.getCharsetName()).toCharArray());
        } catch (Exception e) {
            throw new Exception(e.getMessage(), e);
        }
    }
    /*
    public Object decode( Object object) throws Exception {
        try {
             char[] charArray = (object instanceof String) ? ((String)object).toCharArray() : object;
            return decodeHex(charArray);
        }
        catch (ClassCastException e) {
            throw new Exception(e.getMessage(), e);
        }
    }
     */

    public static byte[] encode(byte[] array) throws UnsupportedEncodingException {
        String string = encodeHexString(array);
        if (string == null) {
            return null;
        }
        return string.getBytes(Hex.charsetName);
    }
    /*
    public Object encode( Object object) throws Exception {
        try {
             byte[] byteArray = (object instanceof String) ? ((String)object).getBytes(this.getCharsetName()) : object;
            return encodeHex(byteArray);
        }
        catch (ClassCastException e) {
            throw new Exception(e.getMessage(), e);
        }
        catch (Exception e2) {
            throw new Exception(e2.getMessage(), e2);
        }
    }
     */

    public String getCharsetName() {
        return Hex.charsetName;
    }

    @Override
    public String toString() {
        return super.toString() + "[charsetName=" + Hex.charsetName + "]";
    }

    static {
        DIGITS_LOWER = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        DIGITS_UPPER = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        Hex.charsetName = "UTF-8";
    }
}
